<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="合同id" prop="contractId">
        <el-input
          v-model="queryParams.contractId"
          placeholder="请输入合同id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上级sheet主键" prop="parentSheetId">
        <el-input
          v-model="queryParams.parentSheetId"
          placeholder="请输入上级sheet主键"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="子目号" prop="zmh">
        <el-input
          v-model="queryParams.zmh"
          placeholder="请输入子目号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="子目名称" prop="zmmc">
        <el-input
          v-model="queryParams.zmmc"
          placeholder="请输入子目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单位" prop="danwei">
        <el-input
          v-model="queryParams.danwei"
          placeholder="请输入单位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['jl_base:baseBaContractSheet:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['jl_base:baseBaContractSheet:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['jl_base:baseBaContractSheet:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['jl_base:baseBaContractSheet:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="baseBaContractSheetList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="sheet主键" align="center" prop="sheetId" v-if="true"/>
      <el-table-column label="合同id" align="center" prop="contractId" />
      <el-table-column label="上级sheet主键" align="center" prop="parentSheetId" />
      <el-table-column label="是否允许修改flag" align="center" prop="updateFlag" />
      <el-table-column label="审核状态" align="center" prop="approveFlag" />
      <el-table-column label="子目号" align="center" prop="zmh" />
      <el-table-column label="子目名称" align="center" prop="zmmc" />
      <el-table-column label="单位" align="center" prop="danwei" />
      <el-table-column label="清单初始单价" align="center" prop="sheetCsPrice" />
      <el-table-column label="清单初始数量" align="center" prop="sheetCsNum" />
      <el-table-column label="清单初始总价" align="center" prop="sheetCsTotalMoney" />
      <el-table-column label="清单初始复核数量" align="center" prop="sheetCsFhNum" />
      <el-table-column label="清单初始复核总价" align="center" prop="sheetCsFhTotalMoney" />
      <el-table-column label="清单单价变化" align="center" prop="sheetPriceBg" />
      <el-table-column label="清单实时单价(=清单初始单价+清单单价变化量)" align="center" prop="sheetSsPrice" />
      <el-table-column label="清单数量变化" align="center" prop="sheetNumBg" />
      <el-table-column label="清单实时数量(=清单初始复核数量+清单数量变化量)" align="center" prop="sheetSsNum" />
      <el-table-column label="清单实时总价(=清单实时数量x清单实时单价)" align="center" prop="sheetSsTotalMoney" />
      <el-table-column label="清单总价变化(=清单实时总价-清单初始复核总价)" align="center" prop="sheetTatalMoneyBg" />
      <el-table-column label="清单初始部分累计计量数量" align="center" prop="sheetCsHavaJlNum" />
      <el-table-column label="清单初始部分累计计量金额(=清单初始部分累计计量数量x清单实时单价)" align="center" prop="sheetCsHaveJlTotalMoney" />
      <el-table-column label="清单变更部分累计计量数量" align="center" prop="sheetBgHavaJlNum" />
      <el-table-column label="清单变更部分累计计量金额(=清单变更部分累计计量数量x清单实时单价)" align="center" prop="sheetBgHaveJlTotalMoney" />
      <el-table-column label="清单累计计量数量(=清单初始部分累计计量数量+清单变更部分累计计量数量)" align="center" prop="sheetHaveJlNum" />
      <el-table-column label="清单累计计量金额(=清单累计计量数量x清单实时单价)" align="center" prop="sheetHaveJlTotalMoney" />
      <el-table-column label="清单初始部分计量审批中数量" align="center" prop="sheetCsJlSpzNum" />
      <el-table-column label="初始部分可计量数量(=清单初始复核数量-清单初始部分累计计量数量-清单初始部分计量审批中数量)" align="center" prop="sheetCsCouldJlNum" />
      <el-table-column label="清单变更部分计量审批中数量" align="center" prop="sheetBgJlSpzNum" />
      <el-table-column label="变更部分可计量数量(=清单数量变化-清单变更部分累计计量数量-清单变更部分计量审批中数量)" align="center" prop="sheetBgCouldJlNum" />
      <el-table-column label="清单实时可计量数量(=变更部分可计量数量+初始部分可计量数量)" align="center" prop="sheetSsCouldJlNum" />
      <el-table-column label="清单实时wbs已经匹配数量" align="center" prop="sheetWbsHavaPpNum" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['jl_base:baseBaContractSheet:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['jl_base:baseBaContractSheet:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改工程量清单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="合同id" prop="contractId">
          <el-input v-model="form.contractId" placeholder="请输入合同id" />
        </el-form-item>
        <el-form-item label="上级sheet主键" prop="parentSheetId">
          <el-input v-model="form.parentSheetId" placeholder="请输入上级sheet主键" />
        </el-form-item>
        <el-form-item label="是否允许修改flag" prop="updateFlag">
          <el-input v-model="form.updateFlag" placeholder="请输入是否允许修改flag" />
        </el-form-item>
        <el-form-item label="审核状态" prop="approveFlag">
          <el-input v-model="form.approveFlag" placeholder="请输入审核状态" />
        </el-form-item>
        <el-form-item label="子目号" prop="zmh">
          <el-input v-model="form.zmh" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="子目名称" prop="zmmc">
          <el-input v-model="form.zmmc" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="单位" prop="danwei">
          <el-input v-model="form.danwei" placeholder="请输入单位" />
        </el-form-item>
        <el-form-item label="清单初始单价" prop="sheetCsPrice">
          <el-input v-model="form.sheetCsPrice" placeholder="请输入清单初始单价" />
        </el-form-item>
        <el-form-item label="清单初始数量" prop="sheetCsNum">
          <el-input v-model="form.sheetCsNum" placeholder="请输入清单初始数量" />
        </el-form-item>
        <el-form-item label="清单初始总价" prop="sheetCsTotalMoney">
          <el-input v-model="form.sheetCsTotalMoney" placeholder="请输入清单初始总价" />
        </el-form-item>
        <el-form-item label="清单初始复核数量" prop="sheetCsFhNum">
          <el-input v-model="form.sheetCsFhNum" placeholder="请输入清单初始复核数量" />
        </el-form-item>
        <el-form-item label="清单初始复核总价" prop="sheetCsFhTotalMoney">
          <el-input v-model="form.sheetCsFhTotalMoney" placeholder="请输入清单初始复核总价" />
        </el-form-item>
        <el-form-item label="清单单价变化" prop="sheetPriceBg">
          <el-input v-model="form.sheetPriceBg" placeholder="请输入清单单价变化" />
        </el-form-item>
        <el-form-item label="清单实时单价(=清单初始单价+清单单价变化量)" prop="sheetSsPrice">
          <el-input v-model="form.sheetSsPrice" placeholder="请输入清单实时单价(=清单初始单价+清单单价变化量)" />
        </el-form-item>
        <el-form-item label="清单数量变化" prop="sheetNumBg">
          <el-input v-model="form.sheetNumBg" placeholder="请输入清单数量变化" />
        </el-form-item>
        <el-form-item label="清单实时数量(=清单初始复核数量+清单数量变化量)" prop="sheetSsNum">
          <el-input v-model="form.sheetSsNum" placeholder="请输入清单实时数量(=清单初始复核数量+清单数量变化量)" />
        </el-form-item>
        <el-form-item label="清单实时总价(=清单实时数量x清单实时单价)" prop="sheetSsTotalMoney">
          <el-input v-model="form.sheetSsTotalMoney" placeholder="请输入清单实时总价(=清单实时数量x清单实时单价)" />
        </el-form-item>
        <el-form-item label="清单总价变化(=清单实时总价-清单初始复核总价)" prop="sheetTatalMoneyBg">
          <el-input v-model="form.sheetTatalMoneyBg" placeholder="请输入清单总价变化(=清单实时总价-清单初始复核总价)" />
        </el-form-item>
        <el-form-item label="清单初始部分累计计量数量" prop="sheetCsHavaJlNum">
          <el-input v-model="form.sheetCsHavaJlNum" placeholder="请输入清单初始部分累计计量数量" />
        </el-form-item>
        <el-form-item label="清单初始部分累计计量金额(=清单初始部分累计计量数量x清单实时单价)" prop="sheetCsHaveJlTotalMoney">
          <el-input v-model="form.sheetCsHaveJlTotalMoney" placeholder="请输入清单初始部分累计计量金额(=清单初始部分累计计量数量x清单实时单价)" />
        </el-form-item>
        <el-form-item label="清单变更部分累计计量数量" prop="sheetBgHavaJlNum">
          <el-input v-model="form.sheetBgHavaJlNum" placeholder="请输入清单变更部分累计计量数量" />
        </el-form-item>
        <el-form-item label="清单变更部分累计计量金额(=清单变更部分累计计量数量x清单实时单价)" prop="sheetBgHaveJlTotalMoney">
          <el-input v-model="form.sheetBgHaveJlTotalMoney" placeholder="请输入清单变更部分累计计量金额(=清单变更部分累计计量数量x清单实时单价)" />
        </el-form-item>
        <el-form-item label="清单累计计量数量(=清单初始部分累计计量数量+清单变更部分累计计量数量)" prop="sheetHaveJlNum">
          <el-input v-model="form.sheetHaveJlNum" placeholder="请输入清单累计计量数量(=清单初始部分累计计量数量+清单变更部分累计计量数量)" />
        </el-form-item>
        <el-form-item label="清单累计计量金额(=清单累计计量数量x清单实时单价)" prop="sheetHaveJlTotalMoney">
          <el-input v-model="form.sheetHaveJlTotalMoney" placeholder="请输入清单累计计量金额(=清单累计计量数量x清单实时单价)" />
        </el-form-item>
        <el-form-item label="清单初始部分计量审批中数量" prop="sheetCsJlSpzNum">
          <el-input v-model="form.sheetCsJlSpzNum" placeholder="请输入清单初始部分计量审批中数量" />
        </el-form-item>
        <el-form-item label="初始部分可计量数量(=清单初始复核数量-清单初始部分累计计量数量-清单初始部分计量审批中数量)" prop="sheetCsCouldJlNum">
          <el-input v-model="form.sheetCsCouldJlNum" placeholder="请输入初始部分可计量数量(=清单初始复核数量-清单初始部分累计计量数量-清单初始部分计量审批中数量)" />
        </el-form-item>
        <el-form-item label="清单变更部分计量审批中数量" prop="sheetBgJlSpzNum">
          <el-input v-model="form.sheetBgJlSpzNum" placeholder="请输入清单变更部分计量审批中数量" />
        </el-form-item>
        <el-form-item label="变更部分可计量数量(=清单数量变化-清单变更部分累计计量数量-清单变更部分计量审批中数量)" prop="sheetBgCouldJlNum">
          <el-input v-model="form.sheetBgCouldJlNum" placeholder="请输入变更部分可计量数量(=清单数量变化-清单变更部分累计计量数量-清单变更部分计量审批中数量)" />
        </el-form-item>
        <el-form-item label="清单实时可计量数量(=变更部分可计量数量+初始部分可计量数量)" prop="sheetSsCouldJlNum">
          <el-input v-model="form.sheetSsCouldJlNum" placeholder="请输入清单实时可计量数量(=变更部分可计量数量+初始部分可计量数量)" />
        </el-form-item>
        <el-form-item label="清单实时wbs已经匹配数量" prop="sheetWbsHavaPpNum">
          <el-input v-model="form.sheetWbsHavaPpNum" placeholder="请输入清单实时wbs已经匹配数量" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBaseBaContractSheet, getBaseBaContractSheet, delBaseBaContractSheet, addBaseBaContractSheet, updateBaseBaContractSheet } from "@/api/jl_base/baseBaContractSheet";

export default {
  name: "BaseBaContractSheet",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工程量清单表格数据
      baseBaContractSheetList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        contractId: undefined,
        parentSheetId: undefined,
        zmh: undefined,
        zmmc: undefined,
        danwei: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        contractId: [
          { required: true, message: "合同id不能为空", trigger: "blur" }
        ],
        parentSheetId: [
          { required: true, message: "上级sheet主键不能为空", trigger: "blur" }
        ],
        zmh: [
          { required: true, message: "子目号不能为空", trigger: "blur" }
        ],
        zmmc: [
          { required: true, message: "子目名称不能为空", trigger: "blur" }
        ],
        danwei: [
          { required: true, message: "单位不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询工程量清单列表 */
    getList() {
      this.loading = true;
      listBaseBaContractSheet(this.queryParams).then(response => {
        this.baseBaContractSheetList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        sheetId: undefined,
        contractId: undefined,
        parentSheetId: undefined,
        updateFlag: undefined,
        approveFlag: undefined,
        zmh: undefined,
        zmmc: undefined,
        danwei: undefined,
        sheetCsPrice: undefined,
        sheetCsNum: undefined,
        sheetCsTotalMoney: undefined,
        sheetCsFhNum: undefined,
        sheetCsFhTotalMoney: undefined,
        sheetPriceBg: undefined,
        sheetSsPrice: undefined,
        sheetNumBg: undefined,
        sheetSsNum: undefined,
        sheetSsTotalMoney: undefined,
        sheetTatalMoneyBg: undefined,
        sheetCsHavaJlNum: undefined,
        sheetCsHaveJlTotalMoney: undefined,
        sheetBgHavaJlNum: undefined,
        sheetBgHaveJlTotalMoney: undefined,
        sheetHaveJlNum: undefined,
        sheetHaveJlTotalMoney: undefined,
        sheetCsJlSpzNum: undefined,
        sheetCsCouldJlNum: undefined,
        sheetBgJlSpzNum: undefined,
        sheetBgCouldJlNum: undefined,
        sheetSsCouldJlNum: undefined,
        sheetWbsHavaPpNum: undefined,
        createTime: undefined,
        createBy: undefined,
        updateTime: undefined,
        updateBy: undefined,
        delFlag: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.sheetId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加工程量清单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const sheetId = row.sheetId || this.ids
      getBaseBaContractSheet(sheetId).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改工程量清单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.sheetId != null) {
            updateBaseBaContractSheet(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addBaseBaContractSheet(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const sheetIds = row.sheetId || this.ids;
      this.$modal.confirm('是否确认删除工程量清单编号为"' + sheetIds + '"的数据项？').then(() => {
        this.loading = true;
        return delBaseBaContractSheet(sheetIds);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('jl_base/baseBaContractSheet/export', {
        ...this.queryParams
      }, `baseBaContractSheet_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
